package com.mygdx.game.newmap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapLayers;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.mygdx.game.GameScreen;

/**
 * Created by dell on 2018/2/6.
 */
public class MapScreen extends ScreenAdapter{


    private final BitmapFont bitmapFont;
    private TiledMap map;

    private OrthogonalTiledMapRenderer renderer;

    private OrthographicCamera camera;

    private Stage stage;
    private SpriteBatch batch;
    private Touchpad touchpad;  //游戏摇杆

    public MapScreen() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);
        batch = new SpriteBatch();
        bitmapFont = new BitmapFont();
        setMap();
        createTouchPad();

    }

    private void createTouchPad() {
        Texture texture = new Texture("data/map2/touchBg.png");
        TextureRegion[][] split = TextureRegion.split(texture, texture.getWidth() / 2, texture.getHeight()/2);
        TextureRegionDrawable touchBg = new TextureRegionDrawable(split[0][0]);
        TextureRegionDrawable knobBg = new TextureRegionDrawable(split[0][1]);
        Touchpad.TouchpadStyle touchpadStyle = new Touchpad.TouchpadStyle(touchBg, knobBg);
        touchpad = new Touchpad(15,touchpadStyle);
        touchpad.setBounds(0,0,150,150);
        stage.addActor(touchpad);
    }

    //摇杆的移动变量
    int speed = 2;
    private int x;
    private int y;

    private void updateTouchPad(){
        if (touchpad.isTouched())
        {
            Gdx.app.error("tag","摇杆移动：x="+touchpad.getKnobPercentX()+" y="+touchpad.getKnobPercentY());
            x+=touchpad.getKnobPercentX()*speed;
            y+=touchpad.getKnobPercentY()*speed;
            Gdx.app.error("tag","摇杆移动：x="+x+" y="+y);
            Person p = (Person) stage.getActors().get(0);

            float k = touchpad.getKnobPercentY() / touchpad.getKnobPercentX();
            if (Math.abs(k)>1&&touchpad.getKnobPercentY()>0)  //向上
            {
                p.setState(Person.STATE.TOP);
            }else if (k<1&&k>-1&&touchpad.getKnobPercentX()>0) //向右
            {
                p.setState(Person.STATE.RIGHT);
            }else if (Math.abs(k)>1&&touchpad.getKnobPercentY()<0) //向下
            {
                p.setState(Person.STATE.BOTTOM);
            }else if (k<1&&k>-1&&touchpad.getKnobPercentX()<0) //向左
            {
                p.setState(Person.STATE.LEFT);
            }
            p.setPosition(x,y);
            /*for (Actor actor : stage.getActors()) {
                if (actor instanceof Person)
                {
                    if (((Person) actor).getState()== Person.STATE.IDLE)
                    {
                    }
                }
            }*/
        }else{
            Person p = (Person) stage.getActors().get(0);
            p.updateState(Person.STATE.IDLE);
        }
    }

    private void setMap() {
        map = new TmxMapLoader().load("data/map2/huoshan.tmx");
        renderer = new OrthogonalTiledMapRenderer(map);  //创建地图渲染
        camera = new OrthographicCamera();
        camera.setToOrtho(false,Main.WORLD_WIDTH,Main.WORLD_HEIGHT); //设置视角

        MapLayers layers = map.getLayers();
        for (MapLayer layer : layers) {

            for (MapObject mapObject : layer.getObjects()) {
                MapProperties properties = mapObject.getProperties();
                Person person = new Person((Float) properties.get("x"),(Float)properties.get("y"));
                if ("per2".equals(mapObject.getName()))
                {
                    person.setState(Person.STATE.LEFT);
                }else if ("per3".equals(mapObject.getName()))
                {
                    person.setState(Person.STATE.TOP);
                }else if ("per4".equals(mapObject.getName()))
                {
                    person.setState(Person.STATE.BOTTOM);
                }else{
                    x = (int) person.getX();
                    y = (int) person.getY();
                }
                stage.addActor(person);

            }
        }
    }

    private float curTime = 0;  //当前游戏运行的时长
    @Override
    public void render(float delta) {
        super.render(delta);
        curTime += delta;
//        Gdx.app.error("tag","time = "+curTime);
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        //实现相机的变化进行渲染
        camera.update();
        renderer.setView(camera);
        renderer.render();
        stage.act();
        stage.draw();
        batch.setColor(1,1,1,1);
        batch.begin();
        bitmapFont.draw(batch,"fps: "+Gdx.graphics.getFramesPerSecond(),10,20);
        batch.end();
        updateTouchPad();
    }

    @Override
    public void dispose() {
        super.dispose();
        map.dispose();
        batch.dispose();
        stage.dispose();
        bitmapFont.dispose();
        renderer.dispose();
    }
}
